<?php

namespace plugin\tuCrontabs\app\model;

use support\facade\Logger;

/**
 *
 */
class CrontabModel extends BaseModel
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
//    protected $connection = 'mysql';
    
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'cms_crontab';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    public $attributes = [
        'running_time',
        'cycle_rule',
        'task_cycle',
    ];

    public function getCreateTimeAttribute($value)
    {
        return getDateText($value);
        return date('Y-m-d H:i:s',$value); // 将时间戳格式化为日期时间字符串
    }


    public function getLastRunningTimeAttribute($value)
    {
        if(!$value) return '--';
        return getDateText($value);
//        return date('Y-m-d H:i:s',$value); // 将时间戳格式化为日期时间字符串
    }

//    public static function getRuleName($data){
//        $cycle_rule = $data->cycle_rule;
//        $task_cycle = $data->task_cycle;
//        $rule_arr = json_decode($cycle_rule,true);
//        if(empty($rule_arr)) return '错误';
//        switch ($task_cycle){
//            case 1:
//                $rule = "每天{$rule_arr['hour']}点{$rule_arr['minute']}分";
//                break;
//            case 2:
//                $rule = "每小时第{$rule_arr['minute']}分";
//                break;
//            case 3:
//                $rule = "{$rule_arr['hour']}小时第{$rule_arr['minute']}分";
//                break;
//            case 4:
//                $rule = "{$rule_arr['minute']}分";
//                break;
//            case 5:
//                $rule = "{$rule_arr['second']}秒";
//                break;
//            case 6:
//                $week_arr = [0=>'周日',1=>'周一',2=>'周二',3 =>'周三',4=>'周四',5=>'周五',6=>'周六'];
//                $rule = "每{$week_arr[$rule_arr['week']]}第{$rule_arr['hour']}点{$rule_arr['minute']}分";
//                break;
//            case 7:
//                $rule = "每月{$rule_arr['day']}号{$rule_arr['hour']}点{$rule_arr['minute']}分";
//                break;
//            case 8:
//                $rule = "每年{$rule_arr['month']}月{$rule_arr['day']}号{$rule_arr['hour']}点{$rule_arr['minute']}分";
//                break;
//            default:
//                $rule = "任务规则不正确";
//        }
//        return $rule;
//    }

    public function getRuleNameAttribute($value){
        $rule_arr = json_decode($this->cycle_rule,true);
        if(empty($rule_arr)) return '错误';
        switch ($this->task_cycle){
                case 1:
                    $rule = "每天{$rule_arr['hour']}点{$rule_arr['minute']}分";
                    break;
                case 2:
                    $rule = "每小时第{$rule_arr['minute']}分";
                    break;
                case 3:
                    $rule = "{$rule_arr['hour']}小时第{$rule_arr['minute']}分";
                    break;
                case 4:
                    $rule = "{$rule_arr['minute']}分";
                    break;
                case 5:
                    $rule = "{$rule_arr['second']}秒";
                    break;
                case 6:
                    $week_arr = [0=>'周日',1=>'周一',2=>'周二',3 =>'周三',4=>'周四',5=>'周五',6=>'周六'];
                    $rule = "每{$week_arr[$rule_arr['week']]}第{$rule_arr['hour']}点{$rule_arr['minute']}分";
                    break;
                case 7:
                    $rule = "每月{$rule_arr['day']}号{$rule_arr['hour']}点{$rule_arr['minute']}分";
                    break;
                case 8:
                    $rule = "每年{$rule_arr['month']}月{$rule_arr['day']}号{$rule_arr['hour']}点{$rule_arr['minute']}分";
                    break;
                default:
                    $rule = "任务规则不正确";
            }
        return $rule;
    }

    // 执行的日志记录
    public function logs(){
        return $this->hasMany(CrontabLogModel::class,'crontab_id','id');
    }
}
